/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import who.tmap.toolkit.shape.PolygonShape;

/**
 * Allow a user to select polygon(s) using their names
 * 
 * @author stl
 */
public class JDialogSelectPolygonByName extends JDialog {

	private javax.swing.JPanel jContentPane = null;
	private JPanel jPanelNames = null;
	private JLabel jLabelNames = null;
	private JList jListNames = null;
	private JPanel jPanelButtons = null;
	private JButton jButtonOK = null;
	private JButton jButtonCancel = null;
	
	private Map<String,PolygonShape> polygons;
	private List<String> selectedPolygons =null;
	
	private JScrollPane jScrollPane = null;
	/**
	 * This is the default constructor
	 */
	public JDialogSelectPolygonByName( Frame parent, boolean modal, Map<String,PolygonShape> polygons ) {
		super( parent, modal );
		
		this.polygons =polygons;
		
		initialize();
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setTitle("Select polygon(s) by name");
		this.setSize(400,300);
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo( null );
		
		getRootPane().setDefaultButton( getJButtonOK() );
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJContentPane() {
		if(jContentPane == null) {
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.setBorder(javax.swing.BorderFactory.createEmptyBorder(5,5,5,5));
			jContentPane.add(getJPanelNames(), java.awt.BorderLayout.CENTER);
			jContentPane.add(getJPanelButtons(), java.awt.BorderLayout.SOUTH);
		}
		return jContentPane;
	}
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanelNames() {
		if (jPanelNames == null) {
			jLabelNames = new JLabel();
			jPanelNames = new JPanel();
			jPanelNames.setLayout(new BorderLayout());
			jLabelNames.setText("Polygons' names");
			jPanelNames.add(jLabelNames, java.awt.BorderLayout.NORTH);
			jPanelNames.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
		}
		return jPanelNames;
	}
	/**
	 * This method initializes jList	
	 * 	
	 * @return javax.swing.JList	
	 */    
	private JList getJListNames() {
		if (jListNames == null) {
			List<String> polygonNames =new ArrayList<String>();

			for( PolygonShape polygon: polygons.values() ) {
				if ( !polygonNames.contains( polygon.getName() ) ) {
					polygonNames.add( polygon.getName() );
				}
			}
			
			jListNames = new JList( polygonNames.toArray() );
		}
		return jListNames;
	}
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanelButtons() {
		if (jPanelButtons == null) {
			jPanelButtons = new JPanel();
			jPanelButtons.add(getJButtonOK(), null);
			jPanelButtons.add(getJButtonCancel(), null);
		}
		return jPanelButtons;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setText("OK");
			jButtonOK.addActionListener(new java.awt.event.ActionListener() { 
				@SuppressWarnings("unchecked")
				public void actionPerformed(java.awt.event.ActionEvent e) {
					List list =java.util.Arrays.asList( jListNames.getSelectedValues() );
				    selectedPolygons =(List<String>)list;
				    setVisible( false );
				}
			});
		}
		return jButtonOK;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setText("Cancel");
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
				    setVisible( false );
				}
			});
		}
		return jButtonCancel;
	}
   
    public List<String> getSelectedPolygons() {
        return selectedPolygons;
    }
	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */    
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJListNames());
		}
		return jScrollPane;
	}
 }
